package com.da.goal.service;

import com.da.common.enums.ResultEnum;
import com.da.common.exception.BusinessException;
import com.da.common.utils.SnowflakeIdWorker;
import com.da.common.utils.TimestampUtil;
import com.da.goal.entity.DailyGoals;
import com.da.goal.mapper.DailyGoalsMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: hyacinth
 * @CreateTime: 2025-02-12
 * @Version: 1.0
 */
@Service("dailyGoalsService")
@RequiredArgsConstructor
@Slf4j
public class DailyGoalsService {

    private final DailyGoalsMapper dailyGoalsMapper;

    /**
     * 创建目标
     * @param dailyGoals 目标实体类
     * @return
     */
    public boolean createGoal(DailyGoals dailyGoals) {
        try{
            String id = String.valueOf(SnowflakeIdWorker.generateId());
            Date createTIme = TimestampUtil.now();
            dailyGoals.setId(id);
            dailyGoals.setCreatedAt(createTIme);
            int i = dailyGoalsMapper.insert(dailyGoals);
            return i > 0;
        }catch (Exception e){
            throw new BusinessException(ResultEnum.FAIL.getCode(), e.getMessage());
        }

    }

    /**
     * 修改项目完成状态
     * @param dailyGoals
     * @return
     */
    public boolean updateGoalCompleted(DailyGoals dailyGoals) {
        try{
            Date updateTime = TimestampUtil.now();
            dailyGoals.setUpdatedAt(updateTime);
            int i = dailyGoalsMapper.update(dailyGoals);
            return i > 0;
        }catch (Exception e){
            throw new BusinessException(ResultEnum.FAIL.getCode(), e.getMessage());
        }
    }





}
